package controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import dto.MemberDTO;

import service.MemberService;
import util.Utilities;

@Controller
public class MemberController {
	private MemberService service;
	
	public MemberController() {
		super();
	}
	public MemberController(MemberService service) {
		this.service = service;
	}
	@RequestMapping("register_form.do")
	public String registerFormController(){
		return "register_form.jsp";
	}
	@RequestMapping("login_form.do")
	public String loginFormController(){
		return "login_form.jsp";
	}
	// 가입 실행
	@RequestMapping("/register.do")
	public ModelAndView registerController(HttpServletRequest request, HttpServletResponse response, MemberDTO mdto) throws Exception{
		try{
			System.out.println(mdto);
			// 생년월일 변환
			String year = request.getParameter("birth_year");
			String month = request.getParameter("birth_month"); 
			String day = request.getParameter("birth_day");
			mdto.setBirthday(Utilities.changeBirthForDB(year, month, day));
			// 가입 시간 저장
			String regist_date = Utilities.getNowForMember();
			mdto.setRegist_date(regist_date);
			mdto.setDateFordisplay(Utilities.changeDayFormat(regist_date));
			service.registerMember(mdto);
		}catch (Exception e) {
			e.printStackTrace();
			new ModelAndView("member.reg","error_message",e.getMessage());
		}
		return new ModelAndView("registerSuccess.res","mdto",mdto); 
	}
	// 로그인 실행
	@RequestMapping("login.do")
	public ModelAndView login(String member_id, String member_password,HttpSession session) throws Exception{
		ModelAndView mv = null;
		try{
			MemberDTO mdto = service.getMemberById(member_id);
			System.out.println(mdto);
			if(mdto!=null){
				// 패스워드 비교
				if(mdto.getMember_password().equals(member_password)){
					session.setAttribute("login_info", mdto);
					mv = new ModelAndView("main.jsp");
				}else{// 패스워드가 틀린 경우
					mv =  new ModelAndView("login_form.jsp","error_message","wrong password!");
				}
			}else{// id 가 없는 경우
				return new ModelAndView("login_form.jsp","error_message","No Id");
			}
		}catch (SQLException e) {
			e.printStackTrace();
			return new ModelAndView("error.res","error_message",e.getMessage());
		}
		return mv;
	}
	// 로그아웃
	@RequestMapping("logout.do")
	public ModelAndView logout(HttpSession session) throws Exception{
		session.invalidate();
		return new ModelAndView("main.jsp");
	}
	// Member 정보
	@RequestMapping("member_detail.do")
	public String member_detail() throws Exception{
		return "member_detail.sub";
	}
	// 
	@RequestMapping("member_update_form.do")
	public ModelAndView member_update(HttpSession session) throws Exception{
		ModelAndView mv;
		try{
			MemberDTO mdto = (MemberDTO) session.getAttribute("login_info");
			mdto = service.getMemberById(mdto.getMember_id());
			mv = new ModelAndView("member_update_form.sub","member",mdto);
		}
		catch (Exception e) {
			e.printStackTrace();
			return mv = new ModelAndView("error.res","error_message",e.getMessage());
		}
		return mv;
	}
}
